package com.atguigu.gmall.all.controller.api;

import com.atguigu.gmall.list.client.feign.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wk
 * @create 2021-09-05 13:53
 * 搜索接口
 */
@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    // 页面 搜索时 触发http://list.gmall.com/list.html?keyword= + this.keyword
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model) {
        // 根据页面需要
        // 1.填充查询对象
        model.addAttribute("searchParam", searchParam);
        SearchResponseVo searchResponseVo = listFeignClient.search(searchParam);
        // 2.填充品牌集合
        model.addAttribute("trademarkList", searchResponseVo.getTrademarkList());
        // 3.平台属性集合
        model.addAttribute("attrsList", searchResponseVo.getAttrsList());
        // 4.商品集合
        model.addAttribute("goodsList", searchResponseVo.getGoodsList());
        // 5.分页集合
        model.addAttribute("pageNo", searchResponseVo.getPageNo());
        model.addAttribute("totalPages", searchResponseVo.getTotalPages());
        // 6.搜索路径参数拼接
        String urlParam = builderUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        // 7. trademarkParam  回显已选中的品牌
        String trademarkParam = builderTrademarkParam(searchParam);
        model.addAttribute("trademarkParam", trademarkParam);
        // 8.已选中平台属性集合 多个值
        List<Map> propsParamList = builderPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);
        // 9.排序
        Map orderMap = builderOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);
        return "list/index";
    }

    private Map builderOrderMap(SearchParam searchParam) {
        String order = searchParam.getOrder();
        Map map = new HashMap();
        // order=1:asc  排序规则   0:asc
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            map.put("type", split[0]);
            map.put("sort", split[1]);
        } else {
            // 如果没有选择排序默认是按照综合排序是根据hotScore排序
            map.put("type", "1");
            map.put("sort", "desc");
        }
        return map;
    }

    // 已选中平台属性集合
    private List<Map> builderPropsParamList(SearchParam searchParam) {
      Map map = new HashMap();
        // props=23:4G:运行内存
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                String[] split = prop.split(":");
                map.put("attrId", split[0]);
                map.put("attrName", split[2]);
                map.put("attrValue", split[1]);
            }
        }
        return Arrays.asList(map);

        // 或者使用stream流 偷天换日
        /*String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            return Arrays.stream(props).map(prop -> {
                Map map = new HashMap();
                String[] split = prop.split(":");
                map.put("attrId", split[0]);
                map.put("attrName", split[2]);
                map.put("attrValue", split[1]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;*/
    }

    // 回显已选中的品牌
    private String builderTrademarkParam(SearchParam searchParam) {
        // trademark=2:华为
        // 页面中数据接收品牌的格式 trademark=2:华为
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            return "品牌" + split[1];
        }
        return null;
    }

    // 拼接 请求路径参数
    private String builderUrlParam(SearchParam searchParam) {
        StringBuilder builder = new StringBuilder();
        String keyword = searchParam.getKeyword();
        // 1.拼接keyword
        builder.append("http://list.gmall.com/list.html?keyword=").append(keyword);
        // 2.品牌
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            builder.append("&trademark=").append(trademark);
        }
        // 3.属性集合
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                builder.append("&props=").append(prop);
            }
        }
        return builder.toString();
    }
}
